package bkx;

import java.io.*;
import java.util.*;

public class shiyan4 {
    public static void main(String args[]){
        File file=new File("C:\\Users\\kexin\\OneDrive\\桌面\\java\\bkx\\商品价格.txt");
        Scanner sc=null;
        int count=0;
        double sum=0;
        try{
            sc=new Scanner(file);
            sc.useDelimiter("[^0123456789.]+");
            while(sc.hasNextDouble()){
                double price = sc.nextDouble();  // 读取价格到 price
                count++;
                sum=sum+price;
                System.out.println(price);

            }
            double aver=sum/count;
            String str=String.format("%.3f",aver);
            System.out.println("平均价格:"+str);
            
            FileWriter fileWriter=new FileWriter(file,true);
            BufferedWriter bufferedwriter= new BufferedWriter(fileWriter);
            bufferedwriter.write("\n平均价格: " + str);
            bufferedwriter.close();
        }
        catch(Exception exp){
            System.out.println(exp);
        }

    }
    
} 